<?php

namespace Home\Controller;

use Home\Model\loginlogModel;

class LoginController extends CommonController{

    public $loginlog;

    /**
     * 构造方法
     */
    public function __construct()
    {
        parent::__construct();
        $this->loginlog = new loginlogModel();
    }

    /**
     * 登陆默认方法
     */
    public function index()
    {
        $this->display('login');
    }

    /**
     * 前台登陆方法
     * @author mmy
     */
    public function login()
    {
        $getArray = I('post.');
        $username = $getArray['username'];
        $password = $getArray['userpassword'];
        $validate_code = $getArray['validate_code'];
        $remember = $getArray['remember'];
        //用户名
        if(empty($username)||strlen($username)<3)
        {
            $this->commonClass->showMessage('用户名不能小于 3 个字母');
        }
        //密码
        if(empty($password)||strlen($password)<6)
        {
            $this->commonClass->showMessage('密码不能小于 6 个字母');
        }
        //验证码
        $res = $this->check_verify($validate_code,'');
        if(empty($validate_code)||!$res)
        {
            $this->commonClass->showMessage('请输入正确的验证码');
        }

        $res = $this->userModel->login($username,md5($password));
        if(count($res)>0)
        {
            $res[0]['realIp'] = $this->getUserIp();
            $res[0]['userid'] =  $res[0]['id'];
            //记录登录用户
            $data = array(
                'userid'=>$res[0]['id'],
                'intime'=>date('Y-m-d H:i:s',time()),
            );
            $logid = $this->loginlog->InsertLoginLog($data);
            $res[0]['loginlogid'] = $logid;
            //记住用户
            if($remember=='on')
            {
                //设置cookie有效时间1个月
               $this->setCookie($res[0]);
            }

            $this->setSession($res[0]);
            redirect('/index.php?c=index&a=index');
        }
        else
        {
            $this->commonClass->showMessage('用户名或密码错误！');
        }
    }

    /**
     * 前台注册页面
     * @author mmy
     *
     */
    public function register()
    {
        $this->display('register');
    }

    /**
     * 注册保存方法
     * @author mmy
     */
    public function registerSave()
    {
        $getArray = I('post.');
        $username = $getArray['username'];
        $isName = $this->userModel->checkIfUserExist($username);
        if($isName)
        {
            $this->commonClass->showMessage('该用户名已存在！');
        }
        $password = $getArray['password'];
        $repassword = $getArray['repassword'];
        $nickname = $getArray['nickname'];
        $validate_code = $getArray['validate_code'];
        //用户名
        if(empty($username)||strlen($username)<3)
        {
            $this->commonClass->showMessage('用户名不能小于 3 个字母');
        }
        //密码
        if(empty($password)||strlen($password)<6)
        {
            $this->commonClass->showMessage('密码不能小于 6 个字母');
        }
        //确认密码
        if(empty($repassword)||strlen($repassword)<6)
        {
            $this->commonClass->showMessage('确认密码不能小于 6 个字母');
        }
        //密码与确认密码是否相等
        if($repassword!=$password)
        {
            $this->commonClass->showMessage('确认密码不能小于 6 个字母');
        }

        //昵称
        if(empty($nickname)||strlen($nickname)<2)
        {
            $this->commonClass->showMessage('昵称不能小于 2 个字母');
        }
        //验证码
        $res = $this->commonClass->check_verify($validate_code);
        if(empty($validate_code)||$res)
        {
            $this->commonClass->showMessage('请输入正确的验证码');
        }
        $data = array(
            'username'=>$username,
            'passwd'=>md5($password),
            'nickname'=>$nickname,
            'ctime'=>date('Y-m-d H:i:s',time()),
            'levelid'=>'1',
        );

        $result = $this->userModel->InsertUser($data);
        if($result)
        {
            $this->commonClass->showMessage('注册成功!','/index.php?c=login&a=index');
        }
        else
        {
            $this->commonClass->showMessage('注册失败！');
        }

    }

    /**
     * 完善用户资料
     * @author mmy
     */
    public function userMessage()
    {
        $this->display('userInfo');
    }

    /**
     * 用户昵称
     * @author mmy
     */
    public function nickname()
    {
        $this->display('nickname');
    }

    /**
     * 修改用户密码
     * @author mmy
     */
    public function modpasswd()
    {
        $this->display('password');
    }

    /**
     * 前台用户中心
     * @author mmy
     */
    public function center()
    {
        $this->display('center');
    }

    /**
     * 文章详细页面
     * @author mmy
     */
    public  function detail()
    {
        $this->display('detail');
    }

    /**
     * 用户退出方法
     */
    public function logout()
    {
        //更新用户登录日志
        $this->loginlog->updateLog(session('HomeUserInfo')['loginlogid']);
        //清掉用户cookie
        $this->Cookie->set('HomeUserInfo', $this->Cookie->authcode(serialize(null), 'ENCODE'), 86400*30);
        session('HomeUserInfo',null);
        redirect('/index.php?c=index&a=index');
    }




}